package com.example.demo_dongruanD.controller;

import com.example.demo_dongruanD.dto.DoctorDetailDto;
import com.example.demo_dongruanD.dto.DoctorLoginRequestDto;
import com.example.demo_dongruanD.pojo.Doctor;
import com.example.demo_dongruanD.pojo.Users;
import com.example.demo_dongruanD.response.ApiResponse;
import com.example.demo_dongruanD.service.DoctorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/doctor")
public class DoctorController {

    private final DoctorService doctorService;

    @Autowired
    public DoctorController(DoctorService doctorService) {
        this.doctorService = doctorService;
    }

    /**
     * 医生登录
     * @param loginRequest
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity<ApiResponse> login(@RequestBody DoctorLoginRequestDto loginRequest) {
        String phone = loginRequest.getPhone();
        String password = loginRequest.getPassword();

        try {
            Doctor doctor = doctorService.findByPhone(phone);

            if (doctor == null) {
                return new ResponseEntity<>(new ApiResponse(600, "账号不对"), HttpStatus.BAD_REQUEST);
            } else if (!doctor.getPassword().equals(password)) {
                return new ResponseEntity<>(new ApiResponse(800, "密码错误"), HttpStatus.BAD_REQUEST);
            } else {
                // 将Doctor实体转换为DoctorDetailDto
                DoctorDetailDto doctorDetail = new DoctorDetailDto();
                doctorDetail.setDocId(doctor.getDocId());
                doctorDetail.setPhone(doctor.getPhone());
                doctorDetail.setDocCode(doctor.getDocCode());

                // 不应包含密码，此处仅做示意
                doctorDetail.setRealName(doctor.getRealName());
                doctorDetail.setPassword(doctor.getPassword());
                doctorDetail.setDeptno(doctor.getDeptno());
                doctorDetail.setSex(doctor.getSex());

                // 返回包含DoctorDetailDto的响应
                return new ResponseEntity<>(new ApiResponse(200, "登录成功", doctorDetail), HttpStatus.OK);
            }
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse(500, "服务器错误"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
